<!--
**********************************************************
* @Author: 张小平
* @Date:   2024-05-01
* @Email:  qqshuqian@163.com
* @Last modified: 最后修改日期
* @Description: 文件描述
**********************************************************
-->
<template>
  <el-container>
    <el-aside width="40%" style="margin-right: 10px;">
      <avue-crud :option="treeOption"
                 v-model:page="treePage"
                 :data="treeData"
                 @on-load="getTreePage"
                 v-model:search="searchFormTree"
                 :permission="getPermission"
                 @row-save="treeSave"
                 @row-update="treeUpdate"
                 @row-del="treeDel"
                 v-model="objTree"
                 @row-click="rowClick"
              >
        <template #radio="{row}">
          <el-radio v-model="selectRowIndex"
                    :label="row.$index"></el-radio>
        </template>
      </avue-crud>
    </el-aside>
    <el-main>
      <avue-crud
              v-model="form"
              v-model:page="page"
              v-model:search="searchForm"
              :permission="getPermission"
              v-bind="bindVal"
              v-on="onEvent"
              :option="option"
              :data="tableData"
              :before-open="beforeOpen"

      ></avue-crud>
    </el-main>
  </el-container>
</template>


<script>

  import {pageCore ,delCore,saveCore} from "@/framework-core/api/core/api";
  import curdMixin from '@/framework-core/mixins/crud';
  import website  from '@/framework-core/config/website';

  export default {
    name: 'know',
    mixins: [curdMixin],
    data () {
      return {
        pageConfig:{"url":"gtp/knowledgepoint",name:"房间"},
        selectRow: {},
        selectRowIndex:"",
        form:{},
        searchFormTree: {},
        searchForm: {},
        objTree: {},
        treeData:[],
        treePage: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: website.website // 每页显示多少条
        },
        treeOption: {
          selectRow: '',
          column: [
            {
              label: '',
              prop: 'radio',
              width: 60,
              hide:true,
              display:false,
            },
            {
              "type": "input",
              "label": "id",
              "prop": "id",
              hide:true,
              display:false,
              "span": 12
            },
         {
              "type": "input",
              label: '知识点',
              prop: 'kname',
              "span": 24
          }],
        },
        tableData: [],
        option: {
          grid: window.isMobile,
          gridSpan: 24,
          column: [
            {
              "type": "input",
              "label": "id",
              "prop": "id",
              hide:true,
              display:false,
              "span": 12
            },
            {
              "type": "input",
              label: '文档表单',
              prop: 'title',
              "span": 24
            },
            {
              "type": "input",
              label: '文档内容',
              prop: 'content',
              "span": 24
            }
          ]
        }
      }
    },
    created () {

    },
    methods: {
      getTreePage() {
        this.loading = true;
        this.treeData = [];
        this.treePage.total = 0;
        var entity = Object.assign(
                {},
                {},
                this.searchFormTree
        );
        var obj = {
          pageNum: this.treePage.currentPage,
          pageSize: this.treePage.pageSize,
        };
        obj.entity = entity;

        pageCore(
                "gtp/knowledge",
                obj
        )
                .then(response => {
                  this.treeData = response.data.data.records;
                  this.treePage.total = response.data.data.total;
                  this.loading = false;
                })
                .catch(() => {
                  this.loading = false;
                });
      },
      treeSave: function(row, done, loading) {
        debugger
        saveCore("gtp/knowledge",row).then(() => {
          this.getTreePage();
          done();
        });
      },

      treeUpdate: function(row, index, done, loading) {
        saveCore("gtp/knowledge",row).then(() => {
          this.getTreePage();
          done();
        });
      },

      treeDel: function(row, index) {
        this.$confirm("是否确认删除：" + row.kname, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delCore("gtp/knowledge", row);
        }).then(() => {
          this.getTreePage();
          this.$message.success("删除成功");
        });
      },
      getPermission: function (key, row, index) {
        // return this.getCommPermission(key, row, index);
        return true;
      },
      beforeOpen(show, type) {
        if (!this.selectRow || !this.selectRow.id) {
          this.$message.success("请选择知识点");
          return
        }
        if (!this.form) {
          this.form = {};
        }
        this.form.kid = this.selectRow.id;
        show();
      },
      rowClick:function (row) {
        this.selectRow =  row;
        this.selectRowIndex =  row.$index;
        this.$message.success(row.kname);
        this.getPage();
      },
      beforParam:function (param) {
        param.kid =this.selectRow?this.selectRow.id:undefined;
      }
    }
  }
</script>
